<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>客户订购系统-购物车</title>
    <link rel="stylesheet" th:href="@{mall/css/iconfont.css}">
    <link rel="stylesheet" th:href="@{mall/css/common.css}">
    <link rel="stylesheet" th:href="@{mall/styles/header.css}">
    <link rel="stylesheet" th:href="@{mall/styles/cart.css}">
    <link rel="stylesheet" th:href="@{/admin/plugins/sweetalert/sweetalert.css}"/>
</head>
<body>
<div id="cart">
    <div class="banner_x center">
        <a th:href="@{/index}" target="_blank">
            <div class="logo fl">
                <img src="mall/image/my-logo-3.png"/>
            </div>
        </a>
        <div class="wdgwc fl ml20">购物车</div>
    </div>
    <div class="cart_bg">
        <th:block th:if="${#lists.isEmpty(myShoppingCartItems)}">
            <div class="list center">
                <img style="position: absolute;margin-top: 16px;left: 45%;" th:src="@{/mall/image/null-content.png}">
            </div>
        </th:block>
        <th:block th:unless="${#lists.isEmpty(myShoppingCartItems)}">
            <div class="list center">
                <div class="top2 center">
                    <div class="sub_top fl">商品名称</div>
                    <div class="sub_top fl">单价</div>
                    <div class="sub_top fl">数量</div>
                    <div class="sub_top fl">小计</div>
                    <div class="sub_top fr">操作</div>
                    <div class="clear"></div>
                </div>
                <th:block th:each="item : ${myShoppingCartItems}">
                    <div class="content2 center">
                        <div class="sub_content fl " th:text="${item.getName()}">商品名称</div>
                        <div class="sub_content fl" th:text="${item.getPrice()+'元'}">1299元</div>
                        <div class="sub_content fl">
                            <input class="goods_count" th:id="${'goodsCount'+item.getProductId()}" type="number"
                                   th:onblur="'updateItem('+${item.getProductId()}+')'"
                                   th:value="${item.getCount()}" step="1" min="1"
                                   max="5">
                        </div>
                        <div class="sub_content fl" th:text="${item.getCount()*item.getPrice()+'元'}">1299元</div>
                        <div class="sub_content fl"><a href="##" th:onclick="'deleteItem('+${item.getProductId()}+')'">×</a>
                        </div>
                        <div class="clear"></div>
                    </div>
                </th:block>
            </div>
        <div class="pre_order mt20 center">
            <div class="order_div fr">
                <div class="order_total fl">合计：<span th:text="${session.cart.getTotalPrice()}+'.00元'">1299.00元</span></div>
                <div class="order_button fr">
                    <a th:href="@{/selectPay}"
                       class="btn btn-small btn-primary" title="去支付">去支付</a>
                </div>
            </div>
        </div>
    </div>
</div>
<div th:replace="mall/footer::footer-fragment"></div>

</body>
<!-- jQuery -->
<script th:src="@{/admin/plugins/jquery/jquery.min.js}"></script>
<script th:src="@{/admin/plugins/sweetalert/sweetalert.min.js}"></script>
<script type="text/javascript">

    /**
     *更新购物项
     */
    function updateItem(id) {
        var domId = 'goodsCount' + id;
        var goodsCount = $("#" + domId).val();
        var data = {
            "cartItemId": id,
            "goodsCount": goodsCount
        };
        $.ajax({
            type: 'PUT',
            url: '/shop-cart',
            contentType: 'application/json',
            data: JSON.stringify(data),
            success: function (result) {
                if (result.resultCode == 200) {
                    window.location.reload();
                } else {
                    swal("操作失败", {
                        icon: "error",
                    });
                }
            },
            error: function () {
                swal("操作失败", {
                    icon: "error",
                });
            }
        });
    }

    /**
     * * 删除购物项
     * @param id
     */
    function deleteItem(id) {
        swal({
            title: "确认弹框",
            text: "确认要删除数据吗?",
            icon: "warning",
            buttons: true,
            dangerMode: true,
        }).then((flag) => {
                if (flag) {
                    $.ajax({
                        type: 'DELETE',
                        url: '/shop-cart/' + id,
                        success: function (result) {
                            if (result.resultCode == 200) {
                                window.location.reload();
                            } else {
                                swal("操作失败", {
                                    icon: "error",
                                });
                            }
                        },
                        error: function () {
                            swal("操作失败", {
                                icon: "error",
                            });
                        }
                    });
                }
            }
        )
        ;
    }
</script>
</html>
